package cloud.dandanit.itservice.controller.video;


import cloud.dandanit.itservice.entity.Image;
import cloud.dandanit.itservice.entity.Video;
import cloud.dandanit.itservice.service.VideoService;
import cloud.dandanit.itservice.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcloud.vod.model.VodUploadResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author luokai
 * @since 2022-07-04
 */
@SuppressWarnings({"DuplicatedCode", "unchecked"})
@RestController
@RequestMapping("/itservice/videofront")
public class VideoFrontController {


    @Autowired
    private VideoService videoService;

    private Long oldPlayCount = 0L;
    private Video oldVideo = null;


    /**
     * 获取前台首页视频方法
     * @return 视频列表
     */
    @GetMapping("getIndexVideo")
    @ApiOperation("获取前台首页视频接口")
    public R getIndexImage() {

        List<Video> videoList = videoService.getIndexVideo();

        return R.ok().data("videoList", videoList);
    }

    @ApiOperation(value = "获取视频分页列表")
    @GetMapping("pageVideo/{page}/{limit}")
    public R getPageVideo(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Video> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getState,1);
        queryWrapper.orderByDesc(Video::getPlayCount).orderByDesc(Video::getSort);
        videoService.page(pageParam,queryWrapper);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    /**
     * 根据id获取视频播放数
     * @return 视频列表
     */
    @GetMapping("getPlayCountOrAddPlayCountById/{videoId}/{flag}")
    @ApiOperation("根据id获取视频播放数接口")
    public R getPlayCountOrAddPlayCountById(
            @ApiParam(name = "videoId", value = "视频ID", required = true)
            @PathVariable String videoId,
            @ApiParam(name = "flag", value = "标志(0查询1自增)", required = true)
            @PathVariable Long flag) {

        //查询
        if (flag == 0) {
            QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",videoId);

            Video video = videoService.getOne(queryWrapper);
            oldVideo = video;
            oldPlayCount = video.getPlayCount();
            return R.ok().data("video", video);
        } else {
            //自增
            oldVideo.setPlayCount(oldPlayCount+1);
            videoService.updateById(oldVideo);
            return R.ok();
        }
    }
}

